package com.taokatao.cardmee.business.profile;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;

import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.WebLayout;
import com.taokatao.cardmee.api.security.ServiceUrlProvider;
import com.taokatao.cardmee.common.widget.ShareDialog;
import com.taokatao.cardmee.common.widget.TitleLayout;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LY on 2016/12/12.
 */

public class ProfileAppShareActivity extends BaseActivity implements WebLayout.Callback, TitleLayout.Callback {
    @BindView(R.id.l_title) TitleLayout lTitle;
    @BindView(R.id.l_web) WebLayout lWeb;

    private ShareDialog dlgShareApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_app_share);
        ButterKnife.bind(this);

        lTitle.setCallback(this);
        lWeb.setCallback(this);
        lWeb.loadUrl(ServiceUrlProvider.obtainAppShareUrl());
    }

    private void setWebTitle(String titleText) {
        Utils.loge("WebActivity setWebTitle %s", titleText);
        if (!TextUtils.isEmpty(titleText)) {
            if (titleText.length() > 10) {
                lTitle.setTitleText(titleText.substring(0, 10) + "..");
            } else {
                lTitle.setTitleText(titleText);
            }
        } else {
            lTitle.setTitleText("");
        }
    }

    private void showShareAppDialog() {
        if (dlgShareApp == null) {
            dlgShareApp = new ShareDialog.Builder(this)
                    .setCallback(new ShareDialog.OnShareClickListener() {
                        @Override
                        public void onPlatformClick(Dialog dialog, SHARE_MEDIA platform) {
                            String appDownUrl =
                                    C.http.APP_DOWNLOAD_URL;
                            String mContent = "亲爱的，快去下载，不听话你就要失去宝宝咯！";
                            if (SHARE_MEDIA.SINA == platform) {
                                mContent = " ";
                            }

                            UMImage image = new UMImage(ProfileAppShareActivity.this, R.mipmap.ic_launcher_s);
                            new ShareAction(ProfileAppShareActivity.this).setPlatform(platform)
                                    .withText(mContent)
                                    .withTitle("卡蜜-拿去，刷我的卡")
                                    .withTargetUrl(appDownUrl)
                                    .withMedia(image)
                                    .setCallback(new UMShareListener() {
                                        @Override
                                        public void onResult(SHARE_MEDIA share_media) {
                                            Utils.onMobEvent(ProfileAppShareActivity.this, "inviteFriendsSucc");
                                            Utils.showToast("分享成功");
                                        }

                                        @Override
                                        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                                            Utils.onMobEvent(ProfileAppShareActivity.this, "inviteFriendsFail");
                                            Utils.showToast("分享失败");
                                        }

                                        @Override
                                        public void onCancel(SHARE_MEDIA share_media) {
                                            Utils.onMobEvent(ProfileAppShareActivity.this, "inviteFriendsFail");
                                            Utils.showToast("分享取消");
                                        }
                                    }).share();
                            dialog.dismiss();
                        }
                    }).create();
        }
        dlgShareApp.show();
    }

    @Override
    public void onPageStarted(boolean isRootPage) {
        setWebTitle(lWeb.webView.getTitle());
    }

    @Override
    public void onPageFinished(boolean isRootPage) {
        setWebTitle(lWeb.webView.getTitle());
    }

    @Override
    public void onMenuAction(String type, String menuAction) {
    }

    @Override
    public void onAppExec(String cmd) {
        if (TextUtils.equals(cmd, "shareApp")) {
            showShareAppDialog();
        }
    }

    @Override
    public void onMenuClick(TitleLayout lTitle, String flag) {
        if (TextUtils.equals(flag, "image")) {
            showShareAppDialog();
        }
    }
}
